package com.upload.service;


import com.github.sd4324530.fastweixin.api.OauthAPI;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.upload.bean.yCoin.LoginInfoBean;
import com.upload.bean.yCoin.LoginRequestBean;
import com.upload.bean.yCoin.LoginResponseBean;
import com.upload.jms.login.LoginClient;
import com.upload.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Created by huangying on 2017/3/28.
 */
@Service
public class LoginService {

    @Autowired
    private RedisTemplate<String,LoginResponseBean> redisTemplate;
    @Value("${CookieExpireTime}")
    private Integer cookieExpireTime;
    @Value("${RedisExpireTime}")
    private Integer redisExpireTime;
    @Autowired
    OauthAPI oauthAPI;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    LoginClient loginClient;

    public LoginResponseBean loginHandler(LoginRequestBean requestBean) {
        LoginResponseBean responseBean = new LoginResponseBean(requestBean.getUnionid());
        if ("12345".equals(requestBean.getUnionid().trim())) {
            responseBean.setExist(true);
            responseBean.setRole("admin");
            responseBean.setNickname("jack");
            responseBean.setHeadImgUrl("https://balalala");
            responseBean.setMsg("登录成功");
            return responseBean;
        } else {
            responseBean.setExist(false);
            responseBean.setMsg("这个用户不存在，登录失败");
            return responseBean;
        }
    }

    public LoginInfoBean loginHandler(HttpServletResponse response, String code) {
        //获取用户信息
        GetUserInfoResponse userInfo = userInfoService.getUserInfo(code);
        LoginResponseBean loginResponseBean = null;

        LoginInfoBean loginInfoBean = new LoginInfoBean();

        try {
//            loginResponseBean = loginClient.sender(new LoginRequestBean("opkdCwNSzMVKj_KhyBZIMdSKhSVc"));
            loginResponseBean = loginClient.sender(new LoginRequestBean(userInfo.getUnionid()));
            if (loginResponseBean != null) {
                if (loginResponseBean.isExist()) {
                    UUID uuid = UUID.randomUUID();
                    CookieUtil cookieUtil = new CookieUtil();
//                    redisTemplate.opsForValue().set(uuid.toString(), loginResponseBean);
//                    redisTemplate.expire(uuid.toString(), redisExpireTime, TimeUnit.SECONDS);
//                    cookieUtil.setCookie("cookieToken", "6666", 20, response);
                    loginInfoBean.setCookieToken(uuid.toString());

                    if ("user".equals(loginResponseBean.getRole())) {
                        loginInfoBean.setStatus("101");
                        loginInfoBean.setRole(loginResponseBean.getRole());
                        loginInfoBean.setNickname(loginResponseBean.getNickname());
                        loginInfoBean.setHeadImgUrl(loginResponseBean.getHeadImgUrl());
                        loginInfoBean.setMsg("用户登录成功");
                        loginInfoBean.setUnionid(loginResponseBean.getUnionid());

                    } else if ("admin".equals(loginResponseBean.getRole())){
                        loginInfoBean.setStatus("102");
                        loginInfoBean.setRole(loginResponseBean.getRole());
                        loginInfoBean.setNickname(loginResponseBean.getNickname());
                        loginInfoBean.setHeadImgUrl(loginResponseBean.getHeadImgUrl());
                        loginInfoBean.setMsg("管理员登录成功");
                        loginInfoBean.setUnionid(loginResponseBean.getUnionid());
                    }
                } else {
                    loginInfoBean.setStatus("103");
                    loginInfoBean.setMsg("用户未注册，请先关注服务号");
                    loginInfoBean.setUnionid(loginResponseBean.getUnionid());
                }
            }
        } catch (JMSException e) {
            loginInfoBean.setStatus("104");
            loginInfoBean.setMsg("登录超时，请重新登录");
            loginInfoBean.setUnionid(loginResponseBean.getUnionid());
            e.printStackTrace();
        }
        return loginInfoBean;
    }
}
